package com.luxiu.extensionlib.utils.dateUtils

import android.content.Context
import android.util.TypedValue
import org.joda.time.DateTime
import org.joda.time.Duration
import java.util.concurrent.TimeUnit
import kotlin.math.abs

class DateUtil {
    companion object {
        const val FULL_PATTERN = "yyyy-MM-dd HH:mm:ss"
        const val DATE_PATTERN = "yyyy-MM-dd"
        const val HOUR_PATTERN = "HH:mm:ss"
        const val WITHOUT_SECOND_PATTERN = "yyyy-MM-dd HH:mm"
    }
}

fun DateTime.isSameDay(dateTime: DateTime): Boolean {
    return this.year == dateTime.year && this.monthOfYear == dateTime.monthOfYear && this.dayOfMonth == dateTime.dayOfMonth
}

fun DateTime.remainDuration(): LongArray {
    val duration = Duration(DateTime.now(), this)
    val days = TimeUnit.MILLISECONDS.toDays(duration.millis)
    val hours = TimeUnit.MILLISECONDS.toHours(duration.millis) % 24
    val minutes = TimeUnit.MILLISECONDS.toMinutes(duration.millis) % 60
    return longArrayOf(days, hours, minutes)
}

/**
 * true: 超时
 * false: 未超时
 */
fun DateTime.remainDurationString(): Pair<String, Boolean> {
    val duration = Duration(DateTime.now(), this)
    val days = TimeUnit.MILLISECONDS.toDays(duration.millis)
    val hours = TimeUnit.MILLISECONDS.toHours(duration.millis) % 24
    val minutes = TimeUnit.MILLISECONDS.toMinutes(duration.millis) % 60
    var timeText = if (days != 0L) "${abs(days)}天" else ""
    timeText += if (hours != 0L) "${abs(hours)}时" else ""
    timeText += "${abs(minutes)}分"
    timeText = (if (minutes < 0) "超时 " else "剩余 ") + timeText
    return Pair(timeText, minutes < 0)
}